package com.workcell.workdata.xthings.mapper.mqtt;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.workcell.workdata.xthings.entity.mqtt.HeatExchangersData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;

@Mapper
@DS("slave_mqtt")
public interface HeatExchangersDataMapper extends BaseMapper<HeatExchangersData> {

	/**
	 * 获取硬件时间区间内的最近一笔数据
	 * @param deviceNo
	 * @return
	 */
	HeatExchangersData getDataHourLineChartInfo(@Param("deviceNo") String deviceNo, @Param("manufacturerCode") String manufacturerCode, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

	/**
	 * 查询报工数据
	 * @param deviceNo
	 * @param manufacturerCode
	 * @param startTime
	 * @param endTime
	 * @return
	 */

	@Select("SELECT *,( " +
			"SELECT FORMAT(( " +
			"SELECT evaporate_Mother_Liquor_Cumulative_Weight FROM t_heat_exchangers_data WHERE slave_device_no=a.slave_device_no AND manufacturer_code=a.manufacturer_code AND `create_time`>=#{startTime} AND `create_time`<=#{endTime} " +
			"ORDER BY `create_time` DESC LIMIT 1)-( " +
			"SELECT evaporate_Mother_Liquor_Cumulative_Weight FROM t_heat_exchangers_data WHERE slave_device_no=a.slave_device_no AND manufacturer_code=a.manufacturer_code AND `create_time`>=#{startTime} " +
			"AND `create_time`<=#{endTime} " +
			"ORDER BY `create_time` ASC LIMIT 1),3)) difEvaporateMotherLiquor " +
			",(  " +
			"SELECT FORMAT(( " +
			"SELECT refrigeration_Liquid_Cumulative_Weight FROM t_heat_exchangers_data WHERE slave_device_no=a.slave_device_no AND manufacturer_code=a.manufacturer_code AND `create_time`>=#{startTime} AND `create_time`<=#{endTime}  " +
			"ORDER BY `create_time` DESC LIMIT 1)-(  " +
			"SELECT refrigeration_Liquid_Cumulative_Weight FROM t_heat_exchangers_data WHERE slave_device_no=a.slave_device_no AND manufacturer_code=a.manufacturer_code AND `create_time`>=#{startTime} " +
			"AND `create_time`<=#{endTime}  " +
			"ORDER BY `create_time` ASC LIMIT 1),3)) difRefrigerationLiquid FROM t_heat_exchangers_data a WHERE id=( " +
			"SELECT MAX(id) aa FROM t_heat_exchangers_data WHERE slave_device_no=#{deviceNo} AND manufacturer_code=#{manufacturerCode} AND `create_time` >= #{startTime} " +
			"AND `create_time`<=#{endTime} );")
	HeatExchangersData getDataHourInfo(@Param("deviceNo") String deviceNo, @Param("manufacturerCode") String manufacturerCode,
								   @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    HeatExchangersData getLatelyDataInfo(@Param("deviceNo")String deviceNo, @Param("manufacturerCode") String manufacturerCode,@Param("time") Integer time);

}
